package com.boby.homecamera.service


import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.Service
import android.content.Intent
import android.content.pm.ServiceInfo
import android.os.Binder
import android.os.Build
import android.os.IBinder
import android.util.Log
import com.boby.homecamera.R
import com.boby.homecamera.constant.Constant
import com.boby.homecamera.constant.Constant.WebSocketCode.FLASHLIGHT_OFF
import com.boby.homecamera.constant.Constant.WebSocketCode.FLASHLIGHT_ON
import com.boby.homecamera.constant.Constant.WebSocketCode.VOICE_CALL_END
import com.boby.homecamera.constant.Constant.WebSocketCode.VOICE_CALL_START
import com.boby.homecamera.data.WebSocketData
import com.boby.homecamera.db.VideoEventTable
import com.boby.homecamera.manager.ClientMsgWebsocketManager
import com.boby.homecamera.service.server.RecordServer
import com.boby.homecamera.webserver.WebServerManager
import com.boby.homecamera.webserver.websocket.MyWebSocketServer
import com.k2fsa.sherpa.onnx.audio.tagging.Tagger
import kotlin.concurrent.thread


class ClientMsgService : Service() {
    companion object{
        val NOTIFICATION_ID: Int = 2
        val CHANNEL_ID: String = "client_msg_channel"
        private val TAG = "ClientMsgService"

        var isRunning = false
            private set
    }


    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        val notificationBuilder: Notification.Builder
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val channel = NotificationChannel(CHANNEL_ID, getString(R.string.app_name), NotificationManager.IMPORTANCE_HIGH)
            val notificationManager = getSystemService(NotificationManager::class.java)
            notificationManager.createNotificationChannel(channel)
            notificationBuilder = Notification.Builder(this, CHANNEL_ID)
        }else{
            notificationBuilder = Notification.Builder(this)
        }

        notificationBuilder.setContentTitle(getString(R.string.app_name))
            .setContentText("正在运行")
            .setSmallIcon(R.mipmap.ic_launcher)
            .build()



        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            startForeground(NOTIFICATION_ID, notificationBuilder.build(),  ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE)
        }else{
            startForeground(NOTIFICATION_ID, notificationBuilder.build())
        }

        Log.d(TAG,"onStartCommand")
        // Your recording logic here
        return START_STICKY
    }
    // 添加一个 Binder 类用于绑定
    inner class LocalBinder : Binder() {
        fun getService(): ClientMsgService = this@ClientMsgService
        fun addClient(deviceId: String, ip: String, port:Int,token: String,listener:((videoEvent: VideoEventTable.VideoEvent) -> Unit)? =  null){
            ClientMsgWebsocketManager.instance.add(deviceId, ip, port,token,listener)
        }
        fun removeClient(deviceId: String){
            ClientMsgWebsocketManager.instance.remove(deviceId)
        }
        fun sendFlashMsg(deviceId: String,flashOn: Boolean){
            ClientMsgWebsocketManager.instance.sendMsg(deviceId,
                WebSocketData(if(flashOn) FLASHLIGHT_ON else FLASHLIGHT_OFF, null).toString())
        }
        fun setAudioCall(deviceId: String,isStart: Boolean){
            ClientMsgWebsocketManager.instance.sendMsg(deviceId,
                WebSocketData(if(isStart) VOICE_CALL_START else VOICE_CALL_END, null).toString())
        }
        fun sendAudioMsg(deviceId: String,byteArray: ByteArray){
            ClientMsgWebsocketManager.instance.sendAudio(deviceId, byteArray)
        }
    }

    private val binder = LocalBinder()

    override fun onBind(intent: Intent?): IBinder? {
        return binder
    }

    override fun onCreate() {
        super.onCreate()
        Log.d(TAG,"onCreate")
        init()
    }

    override fun onDestroy() {
        super.onDestroy()
        isRunning = false


    }

    private fun init(){
//1.启动主页面时启动服务
//2.判断是否需要启动客户端 websocket
//3.退出相机主页时关闭录音服务
//4.退出相机主页关闭判断websocket
//5.退出客户端关闭服务
        ClientMsgWebsocketManager.instance.init()

    }


}